package com.handyshop.mall.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.handyshop.mall.basic.core.constants.BaseConstants;
import com.handyshop.mall.basic.core.constants.ErrorEnum;
import com.handyshop.mall.basic.core.vo.PageBean;
import com.handyshop.mall.basic.core.vo.PageVo;
import com.handyshop.mall.basic.core.vo.ResultVo;
import com.handyshop.mall.dubbo.user.entity.SysOrg;
import com.handyshop.mall.dubbo.user.entity.SysOrgUser;
import com.handyshop.mall.dubbo.user.entity.SysRole;
import com.handyshop.mall.dubbo.user.entity.SysUser;
import com.handyshop.mall.user.mapper.SysOrgMapper;
import com.handyshop.mall.user.service.SysOrgService;
import com.handyshop.mall.user.service.SysOrgUserService;
import com.handyshop.mall.user.service.SysRoleService;
import com.handyshop.mall.user.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hs
 */
@Service
@RequiredArgsConstructor
public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> implements SysOrgService {
    private final SysRoleService sysRoleService;
    private final SysOrgUserService sysOrgUserService;
    private final SysUserService sysUserService;

    /**
     * 根据部门id查询部门员工
     *
     * @param pageBean 分页条件
     * @param id       部门id
     * @return 员工列表
     */
    @Override
    public PageVo<SysUser> findOrgUserById(PageBean pageBean, Long id) {
        LambdaQueryWrapper<SysOrgUser> sysOrgUserWrapper = new LambdaQueryWrapper<>();
        sysOrgUserWrapper.eq(SysOrgUser::getOrgId, id);
        List<SysOrgUser> sysOrgUsers = sysOrgUserService.list(sysOrgUserWrapper);
        if (CollUtil.isEmpty(sysOrgUsers)) {
            return new PageVo<>(0, new ArrayList<>());
        }
        List<Long> userIds = sysOrgUsers.stream().map(SysOrgUser::getUserId).collect(Collectors.toList());

        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.in(SysUser::getId, userIds).eq(SysUser::getDeleteFlag, BaseConstants.NOT_DELETE_FLAG);
        Page<SysUser> page = sysUserService.page(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), sysUserWrapper);
        return new PageVo<>(page.getTotal(), page.getRecords());
    }

    /**
     * 根据id删除部门
     *
     * @return true/成功
     */
    @Override
    public ResultVo<Void> delById(Long id) {
        LambdaQueryWrapper<SysRole> sysRoleWrapper = new LambdaQueryWrapper<>();
        sysRoleWrapper.eq(SysRole::getOrgId, id).eq(SysRole::getDeleteFlag, BaseConstants.NOT_DELETE_FLAG);
        int count = sysRoleService.count(sysRoleWrapper);
        if (count > 0) {
            return ResultVo.failure(ErrorEnum.OPERATION_FAILED, "该部门下有角色,禁止删除!");
        }

        LambdaQueryWrapper<SysOrgUser> sysOrgUserWrapper = new LambdaQueryWrapper<>();
        sysOrgUserWrapper.eq(SysOrgUser::getOrgId, id);
        int orgUserCount = sysOrgUserService.count(sysOrgUserWrapper);
        if (orgUserCount > 0) {
            return ResultVo.failure(ErrorEnum.OPERATION_FAILED, "该部门下有员工,禁止删除!");
        }
        boolean rst = this.removeById(id);
        return ResultVo.success("删除成功");
    }

    /**
     * 根据用户id获取部门
     *
     * @param userId 用户id
     * @return 部门
     */
    @Override
    public List<SysOrg> findByUserId(Long userId) {
        // 获取部门
        LambdaQueryWrapper<SysOrgUser> sysOrgUserWrapper = new LambdaQueryWrapper<>();
        sysOrgUserWrapper.eq(SysOrgUser::getUserId, userId);
        List<SysOrgUser> sysOrgUsers = sysOrgUserService.list(sysOrgUserWrapper);
        if (CollUtil.isEmpty(sysOrgUsers)) {
            return new ArrayList<>();
        }
        List<Long> orgIds = sysOrgUsers.stream().map(SysOrgUser::getOrgId).collect(Collectors.toList());
        LambdaQueryWrapper<SysOrg> sysRoleWrapper = new LambdaQueryWrapper<>();
        sysRoleWrapper.in(SysOrg::getId, orgIds).eq(SysOrg::getDeleteFlag, BaseConstants.NOT_DELETE_FLAG);
        return this.list(sysRoleWrapper);
    }

}
